void RTX::sorted_enqueue(MsgEnv * newNode){
    MsgEnv* cur=timeout_list->head;
    int size=timeout_list->getsize();
    while (timeout_list->head!=NULL){
             if (timeout_list->getsize()==1){;//if only 1 node
                       if (newNode->num_clock_ticks>timeout_list->head->num_clock_ticks){//code to sort the envelope into the timeout_list
                                newNode->next=cur;
                                timeout_list->head=newNode;
                                size++;
                                return;
                       }
                       else if (newNode->num_clock_ticks<timeout_list->head->num_clock_ticks){
                                cur->next=newNode;
                                timeout_list->tail=newNode;
                                size++;
                                return;
                       }
             }
             else{//more than 1 node
                       MsgEnv* A=timeout_list->head;
                       MsgEnv* B=timeout_list->head->next;
                       if (newNode->num_clock_ticks>=A->num_clock_ticks){//case 1:newnode is greater than first node, so it becomes the new node
                                newNode->next=A;
                                timeout_list->head=newNode;
                                timeout_list->tail=newNode;
                                size++;
                                return;
                       }
                       if(newNode->num_clock_ticks<timeout_list->tail->num_clock_ticks){//case 2: newnode is SMALLER THAN LAST NODE, so it becomes the new tail
                                MsgEnv* temp;
                                temp=timeout_list->tail;
                                temp->next=newNode;
                                timeout_list->tail=newNode;
                                timeout_list->tail=newNode;
                                size++;
                                return;
                       }
                       if (newNode->num_clock_ticks > timeout_list->head->num_clock_ticks && newNode->num_clock_ticks < timeout_list->tail->num_clock_ticks){//case 3:its in the middle, traverse until it fits
                                while(A->next!=NULL){
                                      if(newNode->num_clock_ticks > A->num_clock_ticks && newNode->num_clock_ticks < B->num_clock_ticks){
                                           A->next=newNode;
                                           newNode->next=B;
                                           size++;
                                           return;
                                      }
                                      else{
                                           A=A->next;
                                           B=B->next;
                                      }
                                }
                       }
        }
        timeout_list->enqueue(newNode);
}
}
